﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;

namespace Toybox.NicoSharp
{
	using Extensions;
	using Logger;
	using Utilities;

	/// <summary>
	/// CheckListクラス
	/// </summary>
	[DataContract]
	public class CheckList
	{

		#region [Static]

		/// <summary>
		/// CheckListを取得する
		/// </summary>
		/// <param name="cookies"></param>
		/// <returns></returns>
		public static CheckList Get(CookieCollection cookies = null)
		{
			try
			{
				string url = NicoUrls.FlApi + "getchecklist";
				string json = NicoWebClient.Get(url, cookies.Coerce());
				return Deserializer.JsonDeserializer<CheckList>(json);
			}
			catch (Exception ex)
			{
				TraceLogger.WriteLine(ex.ToString());
				return null;
			}
		}

		#endregion [Static]


		#region Public Members

		/// <summary>
		/// 取得結果
		/// </summary>
		[DataMember]
		public string status;

		/// <summary>
		/// コミュニティID配列
		/// </summary>
		[DataMember]
		public string[] community_id;

		/// <summary>
		/// チャンネルID連想配列(ID,Value?)
		/// </summary>
		[DataMember]
		public Dictionary<string, string> channel_id;
		//[IgnoreDataMember]
		//public ChannelId[] channel_id;

		/// <summary>
		/// フレンドID配列
		/// </summary>
		[DataMember]
		public string[] friend;

		/// <summary>
		/// ウォッチリスト配列
		/// </summary>
		[DataMember]
		public string[] watchuser;

		#endregion Public Members

	}

	/// <summary>
	/// CheckListT1クラス
	/// </summary>
	[DataContract]
	public class CheckListT1 : CheckList
	{

		#region [Static]

		/// <summary>
		/// CheckListをt=1で取得する
		/// </summary>
		/// <param name="cookies"></param>
		/// <returns></returns>
		public static new CheckListT1 Get(CookieCollection cookies = null)
		{
			try
			{
				string url = NicoUrls.FlApi + "getchecklist?t=1";
				string json = NicoWebClient.Get(url, cookies.Coerce());
				return Deserializer.JsonDeserializer<CheckListT1>(json);
			}
			catch (Exception ex)
			{
				TraceLogger.WriteLine(ex.ToString());
				return null;
			}
		}

		#endregion [Static]


		#region Public Members

		/// <summary>
		/// フレンドID配列
		/// </summary>
		[DataMember]
		public new UserInfo[] friend;

		/// <summary>
		/// ウォッチリスト配列
		/// </summary>
		[DataMember]
		public new UserInfo[] watchuser;

		#endregion Public Members


		#region [SubClass]

		/// <summary>
		/// ユーザ情報
		/// </summary>
		[DataContract]
		public class UserInfo
		{

			/// <summary>
			/// ユーザID
			/// </summary>
			[DataMember]
			public string user_id;

			/// <summary>
			/// ユーザアイコンの有無
			/// </summary>
			[DataMember]
			public int usericon;

			/// <summary>
			/// 更新時間
			/// </summary>
			[DataMember]
			public string update_time;

			/// <summary>
			/// 更新時間(Date)
			/// </summary>
			[IgnoreDataMember]
			public int UpdateTime
			{
				get
				{
					int time;
					if (int.TryParse(this.update_time, out time))
					{
						return time;
					}
					else
					{
						return -1;
					}
				}
			}
		}

		#endregion [SubClass]

	}
}
